package logic

import (
	"cart_srv/modile/mysql"
	"context"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCartLogic {
	return &CreateCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateCartLogic) CreateCart(in *cart.CreateCartRequest) (*cart.CreateCartResponse, error) {
	// todo: 添加购物车

	c := mysql.CartWeek2{
		UserId:      in.UserId,
		GoodsId:     in.GoodsId,
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  in.GoodsPrice,
		Num:         in.Num,
		GoodsAvatar: in.GoodsAvatar,
	}

	err := c.CreateCart()
	if err != nil {
		logx.Error("新增购物车失败", err)
		return nil, err
	}

	return &cart.CreateCartResponse{CartId: int64(c.ID)}, nil
}
